﻿namespace Example.Views.Info
{
    using System.Web.Mvc;
    using System.Xml.XPath;

    using Cavity;
    using Cavity.Xml.XPath;

    using Example.Models;

    using RazorGenerator.Testing;

    using Xunit;
    using Xunit.Extensions;

    public sealed class HtmlRepresentationFacts
    {
        [Fact]
        public void a_definition()
        {
            Assert.True(new TypeExpectations<HtmlRepresentation>()
                            .DerivesFrom<WebViewPage<InfoViewModel>>()
                            .IsConcreteClass()
                            .IsUnsealed()
                            .HasDefaultConstructor()
                            .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new HtmlRepresentation());
        }

        [Theory]
        [InlineData("1 = count(//dl)")]
        [InlineData("1 = count(//dt[text()='x'])")]
        [InlineData("1 = count(//dd[text()='Example'])")]
        public void title(string xpath)
        {
            Assert.True(render("x", "Example").Evaluate<bool>(xpath));
        }

        private static XPathNavigator render(string id, string title)
        {
            var model = new InfoViewModel
                            {
                                Id = id,
                                Title = title
                            };
            return render(model);
        }

        private static XPathNavigator render(InfoViewModel model)
        {
            var view = new HtmlRepresentation();

            var html = view.RenderAsHtml(model);

            Assert.Equal(model.Title, view.ViewBag.Title);

            return html.CreateNavigator();
        }
    }
}